var intersection = require('array-intersection')
var arrayUnique = require('../20210306/08-arrayUnique')




/**
 * 多个数组的交集
 * @param  {[type]}    arr    [description]
 * @param  {...[type]} values [description]
 * @return {[type]}           [description]
 */
function arrayIntersect(arr, ...values) {
	if (!Array.isArray(arr)) {
	   throw new TypeError('array-unique expects an array.');
	}

	var uniFlag = true
	var array = uniFlag ? arrayUnique(arr) : arr;

	for (var i = 0; i < values.length; i++) {
		array = intersect(array, values[i])
	}

	return array 

	;function intersect(one, two) {
		if(!Array.isArray(one) || !Array.isArray(two)) {
			return []
		}

		var ele, result = []
		for (var i = 0; i < one.length; i++) {
			ele = one[i]
			if(two.includes(ele)) {
				result.push(ele)
			}
		}

		return  result
	}

}

/**
 * 利用数组新函数(filter + every) 求交集 
 * @param  {[type]}    arr    [description]
 * @param  {...[type]} values [description]
 * @return {[type]}           [description]
 */
function arrayIntersectV2(arr, ...values) {
	if (!Array.isArray(arr)) {
	   throw new TypeError('array-unique expects an array.');
	}

	var uniFlag = true
	var arrTwo = uniFlag ? arrayUnique(arr) : arr;
	if(arguments.length === 1) {
		return arrTwo
	}

	return arrTwo.filter(item => {
		return values.every(cur => Array.isArray(cur) && cur.includes(item))
	})
}


// console.log( intersection(33))
console.log( intersection(['a', 'a', 'c']))
//=> ['a', 'c']
console.log( intersection(['a', 'b', 'c', 'b','b'], ['b', 'c', 'e']))
//=> ['b', 'c']

console.log( intersection(['a', 'b', 'c', 'b','b'], ['b', 'c', 'e'], 3))

console.log('-----------------------------------------\n')
console.log( arrayIntersect(['a', 'a', 'c']))
//=> ['a', 'c']
console.log( arrayIntersect(['a', 'b', 'c', 'b','b'], ['b', 'c', 'e']))
//=> ['b', 'c']

console.log( arrayIntersect(['a', 'b', 'c', 'b','b'], ['b', 'c', 'e'], 3))


console.log('-----------------------------------------\n')


console.log( arrayIntersectV2(['a', 'a', 'c']))
console.log( arrayIntersectV2(['a', 'b', 'c', 'b','b'], ['b', 'c', 'e']))
console.log( arrayIntersectV2(['a', 'b', 'c', 'b','b'], ['b', 'c', 'e'], 3))




